/*
 * File Name: 		ARecognize.java
 *
 * Copyright(c) 2011 Yunmai Co.,Ltd.
 *
 * 		 All rights reserved.
 *
 */

package com.ahhwxx.lib.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.ahhwxx.lib.R;
import com.ym.android.engine.pn.OcrEngine;
import com.ym.android.vo.Plate;


/**
 * @author 拍照后识别
 */
public class ARecognize extends AppCompatActivity implements Runnable {

    private TextView mRecoResult;

    private Plate plate = new Plate();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bcr_recognize);

        mRecoResult = (TextView) findViewById(R.id.reco_result);

        new Thread(this).start();

    }

    /**
     * 识别失败
     */
    public static final int RESULT_RECOG_FAILED = 104;
    @SuppressLint("HandlerLeak")
    private Handler mOcrHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case OcrEngine.RECOG_FAIL:
                    Toast.makeText(getBaseContext(), "车牌图像无法识别，请将车牌水平放于框内进行拍摄", Toast.LENGTH_SHORT).show();
                    setResult(RESULT_RECOG_FAILED);
                    finish();
                    break;
                case OcrEngine.RECOG_NONE:
                    setResult(RESULT_RECOG_FAILED);
                    finish();
                    break;
                case OcrEngine.RECOG_BLUR:
                    Toast.makeText(getBaseContext(), "车牌图像无法识别，请将车牌水平放于框内进行拍摄", Toast.LENGTH_SHORT).show();
                    setResult(RESULT_RECOG_FAILED);
                    finish();
                    break;
                case OcrEngine.RECOG_TIME_OUT:
                    Toast.makeText(getBaseContext(), "引擎过期", Toast.LENGTH_LONG).show();
                    setResult(RESULT_RECOG_FAILED);
                    finish();
                    break;
                case OcrEngine.RECOG_LICENSE_ERROR:
                    Toast.makeText(getBaseContext(), "授权失败", Toast.LENGTH_LONG).show();
                    setResult(RESULT_RECOG_FAILED);
                    finish();
                    break;
                case OcrEngine.RECOG_ENGINE_INIT_ERROR:
                    Toast.makeText(getBaseContext(), "引擎初始化失败", Toast.LENGTH_LONG).show();
                    setResult(RESULT_RECOG_FAILED);
                    finish();
                    break;
                case OcrEngine.RECOG_OK:
                    mRecoResult.setVisibility(View.VISIBLE);
                    try {
                        String result = new String(plate.getCharInfo(), "gbk");
                        mRecoResult.setText(result.trim());
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;
            }
        }


    };

    @Override
    public void run() {
        OcrEngine ocrEngine = new OcrEngine();
        try {
            String imagePath = "sdcard/palte.jpg";
            plate = ocrEngine.recognize(this, getIntent().getByteArrayExtra("plateData"), imagePath);
            if (plate.getRecogStatus() == OcrEngine.RECOG_OK) {
                mOcrHandler.sendEmptyMessage(OcrEngine.RECOG_OK);
            } else {
                mOcrHandler.sendEmptyMessage(plate.getRecogStatus());
            }
        } catch (OutOfMemoryError e) {
            mOcrHandler.sendEmptyMessage(OcrEngine.RECOG_FAIL);
        }
    }

}